﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.HeadUpWindow.CApHeadUpWindowManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Windows;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.HeadUpWindow;

public abstract class CApHeadUpWindowManager
{
  private CUiHeadUpWindow m_headUplWindow;

  protected abstract CUiHeadUpWindow CreateWindow();

  public void Display()
  {
    TxGraphicViewer.MouseDoubleClick += new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewer_MouseDoubleClick);
    TxGraphicViewer.MouseDown += new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewer_MouseDown);
    TxApplication.ApplicationEvents.Exiting += new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
  }

  public void Close()
  {
    TxGraphicViewer.MouseDoubleClick -= new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewer_MouseDoubleClick);
    TxGraphicViewer.MouseDown -= new TxGraphicViewer_MouseEventHandler(this.TxGraphicViewer_MouseDown);
    TxApplication.ApplicationEvents.Exiting -= new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
    this.CloseHeadUpWindow();
  }

  private void UpdateHeadUpWindow(System.Drawing.Point topLeft, ITxObject pickedObject)
  {
    if (this.m_headUplWindow == null)
    {
      this.m_headUplWindow = this.CreateWindow();
      ((Window) this.m_headUplWindow).Closed += new EventHandler(this.HeadUplWindow_Closed);
      this.m_headUplWindow.SetObject(pickedObject);
      this.m_headUplWindow.Show(topLeft);
    }
    else
    {
      this.m_headUplWindow.SetObject(pickedObject);
      this.m_headUplWindow.Relocate(topLeft);
    }
  }

  private void HeadUplWindow_Closed(object sender, EventArgs e)
  {
    this.m_headUplWindow = (CUiHeadUpWindow) null;
  }

  private void CloseHeadUpWindow()
  {
    if (this.m_headUplWindow == null)
      return;
    ((Window) this.m_headUplWindow).Close();
  }

  protected virtual bool IsAllowedObject(ITxObject obj) => obj != null;

  private void TxGraphicViewer_MouseDoubleClick(object sender, TxGraphicViewer_MouseEventArgs args)
  {
    if (args.Button != MouseButtons.Left)
      return;
    ITxObject pickedObject = (ITxObject) null;
    TxGraphicPickInfoEx pickInfo = TxGraphicViewerEx.The.GetPickInfo(args.X, args.Y);
    if (pickInfo != null)
      pickedObject = pickInfo.Object;
    if (this.IsAllowedObject(pickedObject))
    {
      System.Drawing.Point topLeft;
      ref System.Drawing.Point local = ref topLeft;
      System.Drawing.Point mousePosition = Control.MousePosition;
      int x = mousePosition.X;
      mousePosition = Control.MousePosition;
      int y = mousePosition.Y;
      local = new System.Drawing.Point(x, y);
      this.UpdateHeadUpWindow(topLeft, pickedObject);
    }
    else
      this.CloseHeadUpWindow();
  }

  private void TxGraphicViewer_MouseDown(object sender, TxGraphicViewer_MouseEventArgs args)
  {
    this.CloseHeadUpWindow();
  }

  private void ApplicationEvents_Exiting(object sender, TxApplication_ExitingEventArgs args)
  {
    this.CloseHeadUpWindow();
  }
}
